package com.ruoyi.lifecycle.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.lifecycle.mapper.LifManageListMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.lifecycle.service.ILifManageListService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-04
 */
@Service
public class LifManageListServiceImpl implements ILifManageListService
{
    private static final Logger log = LoggerFactory.getLogger(LifManageListServiceImpl.class);
    @Autowired
    private LifManageListMapper lifManageListMapper;

    /**
     * 查询单个工艺设备
     *
     * @param deviceId 工艺设备主键
     * @return 【请填写功能名称】
     */
    @Override
    public LifManageList selectLifManageListByDeviceId(Long deviceId)
    {
        return lifManageListMapper.selectLifManageListByDeviceId(deviceId);
    }

    /**
     * 查询工艺设备列表
     *
     * @param lifManageList 工艺设备实体
     * @return 工艺设备列表
     */
    @Override
    public List<LifManageList> selectLifManageListList(LifManageList lifManageList)
    {
        return lifManageListMapper.selectLifManageListList(lifManageList);
    }

    /**
     * 新增工艺设备
     *
     * @param lifManageList 工艺设备实体
     * @return 结果
     */
    @Override
    public int insertLifManageList(LifManageList lifManageList)
    {
        lifManageList.setCreateTime(DateUtils.getNowDate());
        return lifManageListMapper.insertLifManageList(lifManageList);
    }

    /**
     * 修改工艺设备
     *
     * @param lifManageList 工艺设备实体
     * @return 结果
     */
    @Override
    public int updateLifManageList(LifManageList lifManageList)
    {
        lifManageList.setUpdateTime(DateUtils.getNowDate());
        return lifManageListMapper.updateLifManageList(lifManageList);
    }

    /**
     * 批量删除工艺设备
     *
     * @param deviceIds 需要删除的工艺设备主键
     * @return 结果
     */
    @Override
    public int deleteLifManageListByDeviceIds(Long[] deviceIds)
    {
        return lifManageListMapper.deleteLifManageListByDeviceIds(deviceIds);
    }

    /**
     * 删除工艺设备信息
     *
     * @param deviceId 工艺设备主键
     * @return 结果
     */
    @Override
    public int deleteLifManageListByDeviceId(Long deviceId)
    {
        return lifManageListMapper.deleteLifManageListByDeviceId(deviceId);
    }

    /**
     * 导入清单数据
     * @param lifManageLists
     * @param operName
     * @return
     */
    @Override
    public String importList(List<LifManageList> lifManageLists,String operName,Long projectId) {
        if(lifManageLists.size() ==0){
            throw new ServiceException("导入数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder msg = new StringBuilder();
        StringBuilder successMessage = new StringBuilder();
        StringBuilder failureMessage = new StringBuilder();
        for (LifManageList lifManageList : lifManageLists) {
            try {
                //校验是否存在这个设备
                LifManageList list = lifManageListMapper.selectManageByName(lifManageList.getDeviceName());
                if(StringUtils.isNull(list)){
                    lifManageList.setCreateBy(operName);
                    lifManageListMapper.insertLifManageList(lifManageList);
                    successNum++;
                    successMessage.append("<br/>"+successNum+"、"+lifManageList.getDeviceName()+"设备数据导入成功");
                }else{
                    failureNum++;
                    failureMessage.append("<br/>" + failureNum + "、设备名称 " + lifManageList.getDeviceName() + " 已存在");
                }
            } catch (Exception e) {
              failureNum++;
              failureMessage.append("<br/>"+failureNum+"、"+lifManageList.getDeviceName()+"设备数据导入失败"+e.getMessage());
              log.error(failureMessage.toString());
            }
        }
        if(failureNum>0){
            failureMessage.insert(0,"很抱歉，导入失败！共"+ failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMessage.toString());
        }else{
            successMessage.insert(0,"恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMessage.toString();
    }

    @Override
    public List<LifManageList> selectDeviceList(Long projectId) {
        return lifManageListMapper.selectDeviceList(projectId);
    }

    @Override
    public Long updateLifManageListByDeviceId(Long device) {
        return lifManageListMapper.updateLifManageListByDeviceId(device);
    }

    @Override
    public Long updateLifManageListByDeviceById(Long device) {
        return lifManageListMapper.updateLifManageListByDeviceById(device);
    }

    @Override
    public int updateLifManageListByDeviceFlieId(Long device) {
        return lifManageListMapper.updateLifManageListByDeviceFlieId(device);
    }

    @Override
    public int updateMarketState(Long marId) {
        return lifManageListMapper.updateMarketState(marId);
    }

    @Override
    public Long updateMarkeYestState(Long sysDId) {
        return lifManageListMapper.updateMarkeYestState(sysDId);
    }

    @Override
    public Long updateSysMarketStateByDid(Long dId) {
        return lifManageListMapper.updateSysMarketStateByDid(dId);
    }

    @Override
    public Long updateDevicelifeStatusByDeviceId(Long device) {
        return lifManageListMapper.updateDevicelifeStatusByDeviceId(device);
    }

    @Override
    public LifManageList selectLifManageList(Long deviceId) {
        return lifManageListMapper.selectLifManageList(deviceId);
    }

    @Override
    public int updateDeviceStateById(Long deviceId) {
        return lifManageListMapper.updateDeviceStateById(deviceId);
    }

    @Override
    public int updateLifFilFeedBack(LifManageList lifManageList) {
        return lifManageListMapper.updateLifManageList(lifManageList);
    }

    @Override
    public int updateYesState(Long deviceId) {
        return lifManageListMapper.updateYesState(deviceId);
    }

    /*
    * 新增合同信息*/
    @Override
    public int insertSysPurchaseRequisition(LifManageList lifManageList) {
        //从前端获取时间少一天，在这里进行处理（加一天）
        Date CDate = lifManageList.getContractTime();
        Date ODate = lifManageList.getOrderTime();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(CDate);
        calendar.setTime(ODate);
        calendar.add(Calendar.DATE,1);
        lifManageList.setContractTime(calendar.getTime());
        lifManageList.setOrderTime(calendar.getTime());
        return lifManageListMapper.insertSysPurchaseRequisition(lifManageList);
    }

    @Override
    public LifManageList selectSysPurchaseRequisitionByDeviceId(Long deviceId) {
        return lifManageListMapper.selectSysPurchaseRequisitionByDeviceId(deviceId);
    }
}
